package com.wancaijinfu.widget;

import com.wancaijinfu.actitvity.R;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.header.MaterialHeader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ScrollView;

public class MPtrFrameLayout {
	private PtrFrameLayout frameLayout;
	private ScrollView mContentView;
	
	public static MPtrFrameLayout getInstance(Context context, UIInterface infa) {
		MPtrFrameLayout mPtrFrameLayout = new MPtrFrameLayout();
		View view = LayoutInflater.from(context).inflate(R.layout.activity_base_pull, null);
		mPtrFrameLayout.frameLayout = (PtrFrameLayout) view.findViewById(R.id.pull_ptrframelayout);
		mPtrFrameLayout.mContentView = (ScrollView) view.findViewById(R.id.pull__scroll_view);
		mPtrFrameLayout.defaultConfig_Ptr(mPtrFrameLayout.frameLayout, infa);
		return mPtrFrameLayout;
	}
	
	private void defaultConfig_Ptr(final PtrFrameLayout frameLayout, final UIInterface infa) {
		// header
		Context context = frameLayout.getContext();
	
        final MaterialHeader header = new MaterialHeader(context);
        int[] colors = context.getResources().getIntArray(R.array.google_colors);
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        header.setPadding(0, 30, 0, 30);
        header.setPtrFrameLayout(frameLayout);

        frameLayout.setLoadingMinTime(1000);
        frameLayout.setDurationToCloseHeader(1500);
        frameLayout.setHeaderView(header);
        frameLayout.addPtrUIHandler(header);
        
        frameLayout.setPtrHandler(new PtrHandler() {
			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				return PtrDefaultHandler.checkContentCanBePulledDown(frame, mContentView, header);
			}

			@Override
			public void onRefreshBegin(final PtrFrameLayout frame) {
				frame.postDelayed(new Runnable() {
					@Override
					public void run() {
						if (infa != null) infa.getUIData();
					}
				}, 1000);
			}
		});
	}
	
	protected boolean customConfig_Ptr(PtrFrameLayout frameLayout) {
		return false;
	}
	
	public PtrFrameLayout getFrameLayout() {
		return frameLayout;
	}
	
	public ScrollView getContextView() {
		return mContentView;
	}
}
